package com.liang.pcc.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.pcc.domain.TopologyDataDTO;
import com.liang.pcc.mapper.TopologyDataMapper;
import com.liang.pcc.service.AuthUserService;
import com.liang.pcc.service.TopologyDataService;
import com.liang.pcc.util.TreeCalculator;
import com.liang.pcc.util.TreeNode;
import com.liang.pcc.util.TreeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* 设备层级表Service实现
*/
@Service
@RequiredArgsConstructor
public class TopologyDataServiceImpl extends ServiceImpl<TopologyDataMapper, TopologyDataDTO> implements TopologyDataService{

    private final AuthUserService userService;

    @Override
    public List<TreeNode> getTopologyData(Long fileId) {
        Long userId = userService.getUserIdByAccount(StpUtil.getLoginId().toString());
        List<TopologyDataDTO> list = lambdaQuery()
                .eq(TopologyDataDTO::getFileId, fileId)
                .eq(TopologyDataDTO::getUserId, userId)
                .list();
        List<TreeNode> data = TreeUtil.buildTree(list);
        TreeCalculator.calculateBusValues(data);
        return data;
    }

    @Override
    public List<TreeNode> getTopologyData(Long fileId, Long userId) {
        List<TopologyDataDTO> list = lambdaQuery()
                .eq(TopologyDataDTO::getFileId, fileId)
                .eq(TopologyDataDTO::getUserId, userId)
                .list();
        List<TreeNode> data = TreeUtil.buildTree(list);
        TreeCalculator.calculateBusValues(data);
        return data;
    }
}




